package com.jeeplus.modules.cms.statistic.service.impl;

import com.jeeplus.cms.common.CmsConstant;
import com.jeeplus.modules.cms.column.entity.CmsColumn;
import com.jeeplus.modules.cms.site.service.CmsSiteService;
import com.jeeplus.modules.cms.statistic.mapper.StatisticMapper;
import com.jeeplus.modules.cms.statistic.service.StatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author Sawyer
 * @date 2020-11-25
 */
@Service
public class StatisticServiceImpl implements StatisticService {

    @Autowired
    private StatisticMapper statisticMapper;

    @Autowired
    private CmsSiteService cmsSiteService;

    @Override
    public List<CmsColumn> hitsStats(CmsColumn column) {
        if (null == column) {
            column = new CmsColumn();
        }

        if (null == column.getParent()) {
            column.setParent(new CmsColumn("0"));
        }

        if (null == column.getParent().getId() || "-1".equals(column.getParent().getId())) {
            column.getParent().setId("0");
        }

        List<CmsColumn> list = statisticMapper.hitsStats(column);
        list = null == list ? Collections.emptyList() : list;
        //从根节点开始查找时手动添加首页信息
        if (list.size() > 0 && "0".equals(column.getParentId())) {
            CmsColumn homeColumn = new CmsColumn();
            homeColumn.setId("-1");
            homeColumn.setName("首页");
            homeColumn.setSort(0);
            homeColumn.setParent(new CmsColumn("0"));
            homeColumn.setImage("fa fa-home");
            homeColumn.setEnableFlag(CmsConstant.YES);
            homeColumn.setHits(cmsSiteService.getHomeHits(column.getSiteId()));
            homeColumn.setInfoCount(0);
            homeColumn.setDetailsHits(0);
            list.add(0, homeColumn);
        }
        return list;
    }
}
